﻿using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using NOSDC.Utility;

namespace NOSDC
{
    public class MvcApplication : HttpApplication
    {
        protected void Application_Error()
        {
            var exception = Server.GetLastError();
            Response.Clear();

            var httpException = exception as HttpException;

            if (httpException != null)
            {
                string action;

                switch (httpException.GetHttpCode())
                {
                    case 404:
                        // page not found
                        action = "Http404";
                        break;
                    case 500:
                        // server error
                        action = "Http500";
                        Mail.SendBugEmail(exception, "");
                        break;
                    default:
                        action = "Error";
                        break;
                }
                #if !DEBUG
                Server.ClearError();
                Response.Redirect(String.Format("~/Error/{0}", action));
                #endif
            }
        }
         
        public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {
            filters.Add(new HandleErrorAttribute());
        }

        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute("Events", // Route name 
                "Events/{EventID}/{Title}", // URL with parameters 
                new { controller = "Events", action = "Index", id = "{EventID}", eventName = "{Title}" }, // Parameter defaults );
                 new[] {"NOSDC.Controllers"});

            routes.MapRoute("Article", // Route name 
                "Article/{ArticleID}/{Title}", // URL with parameters 
                new { controller = "Article", action = "FullArticle", id = "{ArticleID}", articleName = "{Title}" }); // Parameter defaults );

            routes.MapRoute("Jobs", "Jobs/{JobId}/{Title}", new { controller = "Jobs", action = "Details", id = "{JobID}", title = "{Title}" },
                new[] { "NOSDC.Controllers", "NOSDC.Controllers.JobsController" }); 

            routes.MapRoute(
                "Default", // Route name
                "{controller}/{action}/{id}", // URL with parameters
                new { controller = "Home", action = "Index", id = UrlParameter.Optional }, // Parameter defaults
                new[] { "NOSDC.Controllers", "NOSDC.Controllers.JobsController" }
            );
        }

        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();

            RegisterGlobalFilters(GlobalFilters.Filters);
            RegisterRoutes(RouteTable.Routes);
        }
    }
}